//封装购物车模块
import { defineStore } from "pinia";
import { ref, computed } from "vue";
import { useUserStore } from "./userStore";
import { insertCartAPI, findNewCartListAPI, delCartAPI } from "@/apis/cart";
import { ElMessage } from "element-plus";

export const useCartStore = defineStore('cart', () => {
    //1.定义state
    const cartList = ref([])
    // 2.定义action
    // 获取最新购物车列表action
    const updateNewList = async () => {
        const res = await findNewCartListAPI()
        cartList.value = res.data
    }
    const userStore = useUserStore()
    const isLogin = computed(() => userStore.userInfo.data?.token)
    //添加购物车操作
    const addCart = async (goods) => {
        const { goodsId, count } = goods
        if (isLogin.value) {
            //已经登录
            const res = await insertCartAPI({ goodsId, count })
            //提示是否加入成功
            ElMessage.success(res.data.data)
            updateNewList()
        } else {
            //已经添加过这个商品 - count + count
            //没有加过 - 加一个
            const item = cartList.value.find((item) => goods.goodsId === item.goodsId)
            if (item) {
                //找到了
                item.count = goods.count + item.count
                console.log(cartList.value);
            } else {
                //没找到
                cartList.value.push(goods)
            }
            ElMessage.success("添加购物车成功")
        }
    }
    //删除购物车中的一条
    const delCart = async (goodsId) => {
        if (isLogin.value) {
            await delCartAPI([goodsId])
            updateNewList()

        } else {
            // 思路：
            // 1. 找到要删除项的下标值 - splice
            // 2. 使用数组的过滤方法 - filter
            const idx = cartList.value.findIndex((item) => goodsId === item.goodsId)
            cartList.value.splice(idx, 1)
        }
    }

    // 清除购物车
    const clearCart = () => {
        cartList.value = []
    }

    //单选功能
    const singleCheck = (goodsId, selected) => {
        // 通过skuId找到要修改的那一项 然后把它的selected修改为传过来的selected
        const item = cartList.value.find((item) => item.goodsId === goodsId)
        item.selected = selected
    }
    // 全选功能
    const allCheck = (selected) => {
        // 把cartList中的每一项的selected都设置为当前的全选框状态
        cartList.value.forEach(item => item.selected = selected)
    }

    // 计算属性
    // 1. 总的数量 所有项的count之和
    const allCount = computed(() => cartList.value.reduce((a, c) => a + c.count, 0))
    // 2. 总价 所有项的count*price之和
    const allPrice = computed(() => cartList.value.reduce((a, c) => a + c.count * c.price, 0))
    // 3. 已选择数量
    const selectedCount = computed(() => cartList.value.filter(item => item.selected).reduce((a, c) => a + c.count, 0))
    // 4. 已选择商品价钱合计
    const selectedPrice = computed(() => cartList.value.filter(item => item.selected).reduce((a, c) => a + c.count * c.price, 0))
    // 5. 已选择商品运费价钱合计
    const selectedShippingFee =computed(() => cartList.value.filter(item => item.selected).reduce((a, c) => a + c.shippingFee, 0))
    // 6. 已经选择的总价钱
    const selectedTotalPrice =computed(() => cartList.value.filter(item => item.selected).reduce((a, c) => a + c.count * c.price +c.shippingFee, 0))
    // 是否全选
    const isAll = computed(() => cartList.value.every((item) => item.selected))

    return {
        cartList,
        allCount,
        allPrice,
        isAll,
        selectedCount,
        selectedPrice,
        selectedShippingFee,
        selectedTotalPrice,
        addCart,
        delCart,
        singleCheck,
        allCheck,
        clearCart,
        updateNewList
    }
}, {
    persist: true//持久化存储
})